home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2010 April / PCWorld0410.iso / hity wydania / Ubuntu 9.10 PL / karmelkowy-koliberek-9.10-netbook-remix-PL.iso / casper / filesystem.squashfs / usr / share / pyshared / nevow / events.py < prev    next >
Text File  |  2005-05-16  |  1KB  |  55 lines

  1. # Copyright (c) 2004 Divmod.
  2. # See LICENSE for details.
  3.  
  4.  
  5. DEBUG = False
  6.  
  7.  
  8. class EventNotification:
  9.     def __init__(self):
  10.         self._subscribers = {}
  11.         self._currentId = 0
  12.  
  13.     def subscribe(self, identifier, subscriber):
  14.         """Subscribe to events sent to the given identifier.
  15.         
  16.         Returns a token which should be passed to unsubscribe when done.
  17.         """
  18.         if DEBUG:
  19.             print "SUBSCRIBE", self, identifier, subscriber
  20.         self._subscribers.setdefault(identifier, []).append(subscriber)
  21.         return identifier, subscriber
  22.  
  23.     def unsubscribe(self, token):
  24.         """Unsubscribe the given token from events.
  25.         """
  26.         if DEBUG:
  27.             print "UNSUBSCRIBE", token
  28.         identifier, reference = token
  29.         self._subscribers[identifier].remove(reference)
  30.  
  31.     def publish(self, identifier, *args):
  32.         """Notify the listeners on a given identifier that an event has occurred.
  33.         """
  34.         if DEBUG:
  35.             print "PUBLISH", self, identifier,
  36.         subscribers = self._subscribers.get(identifier, [])
  37.         for sub in subscribers:
  38.             sub(*args)
  39.             if DEBUG:
  40.                 print "NOTIFY SUBSCRIBER", sub
  41.         if DEBUG:
  42.             print "done"
  43.  
  44.     def nextId(self):
  45.         self._currentId += 1
  46.         return str(self._currentId)
  47.  
  48.     def __getstate__(self):
  49.         d = self.__dict__.copy()
  50.         d['_subscribers'] = {}
  51.         d['_currentId'] = 0
  52.         return d
  53.  
  54.  
  55.